package com.ssm.service.system.impl;

import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.ssm.common.Message;
import com.ssm.dao.system.OperatorDao;
import com.ssm.entity.system.Operator;
import com.ssm.service.system.OperatorService;
import com.ssm.util.MD5;
import com.ssm.web.Page;

@Service
public class OperatorServiceImpl implements OperatorService{
	
	@Autowired
	private OperatorDao operatorDao;

	@Override
	public Page<Operator> queryList(Operator operator) {
		Page<Operator> page = new Page<>();
		try {
			page.setCount(operatorDao.queryCount(operator));
			page.setRecord(operatorDao.queryList(operator));
			page.setCurrentPage(operator.getCurrentPage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

	@Override
	public Operator getDetail(String operatorId) {
		return operatorDao.getDetail(operatorId);
	}

	@Override
	public Message save(Operator operator) {
		Message message = new Message();
		try {
			operator.setCreater("admin");
			operator.setPwd(new MD5().encrypt(operator.getPwd()));
			operatorDao.save(operator);
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}

	@Override
	public Message delete(String[] ids) {
		Message message = new Message();
		try {
			String loginId = "";/*CommonUtil.getOperatorId();*/
			for (String id : ids) {
				if (id.equals(loginId)) {
					message.setSuccess(false);
					message.setMessage("不能删除自己");
					return message;
				}
			}
			operatorDao.delete(ids);
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}

	@Override
	public Message update(Operator operator) {
		Message message = new Message();
		try {
			operatorDao.update(operator);
		} catch (DataAccessException e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("操作失败");
		}
		return message;
	}

	@Override
	public Message updatePwd(Operator operator, String newPwd) {
		Message message = new Message();
//		Operator oper = CommonUtil.getOperator();
//		if (!new MD5().encrypt(operator.getPwd()).equals(oper.getPwd())) {
//			message.setSuccess(false);
//			message.setMessage("密码错误");
//			return message;
//		}
//		
//		oper.setPwd(new MD5().encrypt(newPwd));
//		operatorDao.updatePwd(oper);
		return message;
	}

	@Override
	public Message imports(InputStream inputStream) {
		Message message = new Message();
		Operator operator = null;
		try {
			HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputStream);
			for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
				HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
	            if (hssfSheet == null) {
	                continue;
	            }
	            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
	            	operator = new Operator();
	            	HSSFRow hssfRow = hssfSheet.getRow(rowNum);
	            	operator.setId(hssfRow.getCell(0).getStringCellValue());
	            	operator.setPwd(hssfRow.getCell(1).getStringCellValue());
	            	operator.setName(hssfRow.getCell(2).getStringCellValue());
	            	operator.setSex(Integer.valueOf(hssfRow.getCell(3).getStringCellValue()));
	            	operator.setBirthDate(hssfRow.getCell(4).getDateCellValue());
	            	operator.setAddress(hssfRow.getCell(8).getStringCellValue());
	            	operator.setMobile(hssfRow.getCell(9).getStringCellValue());
	            	operator.setPhone(hssfRow.getCell(10).getStringCellValue());
	            	operator.setRoleId(hssfRow.getCell(11).getStringCellValue());
	            	operator.setCreater("admin");
	            	operatorDao.save(operator);
	            }
			}
		} catch (Exception e) {
			e.printStackTrace();
			message.setSuccess(false);
			message.setMessage("导入失败");
		}
		return message;
	}
}
